﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace FriendsNearbyBot
{
    public class SingleInstanceRunner
    {
        string identifier;
        Action normalAction;
        Action forceExitAction;

        public SingleInstanceRunner(string identifier, Action normalAction, Action forceExitAction)
        {
            this.identifier = identifier;
            this.normalAction = normalAction;
            this.forceExitAction = forceExitAction;
        }

        public void Run()
        {
            using (var mutex = new Mutex(false, identifier))
            {
                if (mutex.WaitOne(0, false))
                {
                    normalAction();
                }
                else
                {
                    forceExitAction();
                }
            }
        }
    }
}
